<?php defined('SYSPATH') OR die('No direct script access.');

class Controller_Qr extends Controller {

	public function action_index($text, $size = 25, $watermark = '')
	{
		return $this->action_png($text, $size, $watermark);
	}

	public function action_png($text, $size = 25, $watermark = '')
	{
		//$this->response->headers('Content-Type', 'image/png');
		if (empty($watermark) || !file_exists(APPPATH.$watermark))
			QR::png($text, FALSE, QR_ECLEVEL_M, $size, 0 );
		else
		{
			$file = APPPATH.$watermark;
			$driver = class_exists('Imagick') ? 'Imagick' : NULL;
			$tmp = tempnam(sys_get_temp_dir(), '.png');
			QR::png($text, $tmp, QR_ECLEVEL_M, $size, 0 );
			$img = Image::factory($tmp, $driver);
			$mark = Image::factory($file, $driver);
			$mark->resize($img->width * 0.2, $img->height * 0.2);
			$img->watermark($mark,NULL, NULL, 100);
			echo $img->render();
			unlink($tmp);
		}
	}

	public function action_svg($text, $element_id = FALSE, $width = FALSE, $size = FALSE)
	{
		echo QR::svg($text, $element_id, FALSE, QR_ECLEVEL_H, $width, $size, 0 );
	}
}